﻿using System;
using System.Globalization;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace ShopPowerTemplate.Common
{
    /// <summary>
    /// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
    /// <see cref="Visibility.Collapsed"/>.
    /// </summary>
    public sealed class IntegerToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string result = string.Empty;
            if (value is Int32)
            {
                CultureInfo cultureInfo = string.IsNullOrWhiteSpace(language) ? CultureInfo.CurrentUICulture : new CultureInfo(language);
                Int32 integerValue = ((Int32)value);
                result = integerValue.ToString(cultureInfo.NumberFormat);
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            Int32 result = 0;
            if (value is string)
            {
                string decimalString = ((string)value);
                if (!string.IsNullOrWhiteSpace(decimalString))
                {
                    CultureInfo cultureInfo = string.IsNullOrWhiteSpace(language) ? CultureInfo.CurrentUICulture : new CultureInfo(language);
                    Int32.TryParse(decimalString, NumberStyles.Any, cultureInfo.NumberFormat, out result);
                }
            }
            return result;
        }
    }
}
